package com.demo.boss.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.boss.common.convert.UserPayConfigConvert;
import com.demo.boss.common.enums.SysCode;
import com.demo.boss.entity.UserPayConfig;
import com.demo.boss.entity.UserPayInfo;
import com.demo.boss.models.PageRequest;
import com.demo.boss.models.Result;
import com.demo.boss.models.request.UserProductConfigRequest;
import com.demo.boss.server.UserPaymentConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
public class UserPaymentConfigController {
    @Resource
    private UserPaymentConfigService userPaymentConfigService;
    @Resource
    UserPayConfigConvert userPayConfigConvert;
    @GetMapping("/pay/configList")
    public Result configPageList(@Validated PageRequest pageRequest){
        IPage<UserPayConfig> userIPage = userPaymentConfigService.pageConfigList(pageRequest);
        Map<String,Object> data = new HashMap<>();

        data.put("current",userIPage.getCurrent());
        data.put("pages",userIPage.getPages());
        data.put("list",userPayConfigConvert.mapUserPayConfigList(userIPage.getRecords()));
        return Result.ok(data);
    }
    /**
     * 创建商户支付产品
     */
    @PostMapping("/config/create")
    public Result createUserProductConfig(@RequestBody UserProductConfigRequest configRequest){
        userPaymentConfigService.createConfig(configRequest);
        return Result.ok();
    }
    /**
     *
     */
    @GetMapping("/config/getPayInfo")
    public Result queryPayInfo(String userNo,String productCode){
        if(StrUtil.isNotBlank(userNo) && StrUtil.isNotBlank(productCode)){
            List<UserPayInfo> userPayInfos =  userPaymentConfigService.queryPayInfo(userNo,productCode);
            return Result.ok(userPayConfigConvert.mapUserPayInfo(userPayInfos));
        }
        return Result.fail(SysCode.REQUEST_PARAM_ERROR);
    }
}
